<?php
/**
 * 会员系统    暂时不支持 系统组授权
 * groupid系统组,verify为验证级别 ,coupons为自定义会员组
 * 功能:会员的添加、删除、查看信息、授权
 *@author wolf  [Email: 116311316@qq.com]
 *@since 2011-07-20  2014-08-04
 *@version 3.0 第3次简化
 */
class MemberController extends NodeController {
	
	// 配置用户列表 导入
	public function listing() {
		$cid = 1;
		/* 分页如何缓存 */
		if (isset ( $_GET ['p'] )) {
			$cache_id = 'p' . $_GET ['p'];
		} else {
			$cache_id = $cid;
		}
		$member = self::getMemberService ()->listing ( $_GET ['p'], $_GET );
		$verify = isset ( $_GET ['verify'] ) ? 0 : $_GET ['verify'];
		$this->view ()->assign ( 'verify', array ('id' => $verify, 'name' => $member ['verify'] [$verify] ) );
		$groupid = isset ( $_GET ['gruopid'] ) ? 0 : $_GET ['groupid'];
		$this->view ()->assign ( "groupid", array ('id' => $groupid, 'name' => $member ['groupname'] [$groupid] ) );
		$this->view ()->assign ( 'num', $member ['page'] );
		$this->view ()->assign ( 'totalnum', $member ['totalnum'] );
		$this->view ()->assign ( 'news', $member ['memberlist'] );
		// 在线导出功能
		$this->view ()->assign ( 'cate', $this->getMemberService ()->_verify );
		$this->view ()->assign ( "group", $member ['groupname'] );
		$this->view ()->display ( 'file:member/mlist.tpl' );
	}
	/**
	 * 删除会员
	 * Enter description here ...
	 */
	public function remove() {
		$rs = self::getMemberService ()->remove ( $_POST ['uid'] );
		self::getLogService ()->add ( $this->_user_global ['uid'], "删除会员$_POST[uid]" );
		
		$this->sendNotice ( $rs, null, true );
	}
	/**
	 * 审核头像
	 * Enter description here ...
	 */
	public function vaildFace() {
		$id = $_POST ['id'];
		//验证头像
		$type = $_POST ['type'];
		$rs = $this->getMemberService ()->validFace ( $id, $type );
		$this->sendNotice ( $rs );
	
	}
	
	/**
	 * /member/setface/    filename face
	 * 流上传服务
	 */
	public function setFace() {
		$user = unserialize ( $_SESSION ['uid'] );
		$uid = $user ['uid'];
		$userinfo = MemberModel::instance ()->getOneMember ( array ('uid' => $uid ) );
		$this->getMemberService ()->uploadFace ( $uid, $userinfo ['real_name'] );
	}
	/**
	 * 头像审核列表
	 * Enter description here ...
	 */
	public function faceList() {
		$rs = self::getMemberService ()->getVaildFaceList ();
		$this->view ()->assign ( 'list', $rs );
		$this->view ()->display ( 'file:member/flist.tpl' );
	}
	/**
	 * 查询用户系统
	 */
	public function cSearch() {
		$rs = self::getMemberService ()->search ( $_POST ['type'], $_POST ['value'] );
		$this->view ()->assign ( 'news', $rs );
		$this->view ()->assign ( 'cate', self::getMemberService ()->_verify );
		$this->view ()->display ( 'file:member/mlist.tpl' );
	}
	
	/**
	 * 对单个用户进行编辑
	 */
	public function add() {
		$group = self::getMemberService ()->getMemberGroup ();
		$pwd = self::getMemberService ()->randstr ();
		$this->view ()->assign ( 'verify', self::getMemberService ()->_verify );
		$this->view ()->assign ( 'password', $pwd );
		$this->view ()->assign ( 'group', $group );
		$this->view ()->display ( 'file:member/add.tpl' );
	}
	/**
	 * 新增用户
	 */
	public function addUser() {
		$rs = self::getMemberService ()->register ( $_POST );
		self::getLogService ()->add ( $this->_user_global ['uid'], "添加用户$_POST[username]" );
		$this->sendNotice ( $rs, null, false );
	}
	
	/**
	 * 编辑用户
	 */
	public function edit() {

		$rs = self::getMemberService ()->getMemberByUid ( $_GET ['id'] );
		$group = self::getMemberService ()->getGroup ();
		//载入日志记录
		$log = self::getLogService ()->getLogByUsername ( $rs ['mobile_phone'] );
		$this->view ()->assign ( 'log', $log );
		$this->view ()->assign ( 'group', $group );
		$this->view ()->assign ( "verify", self::getMemberService ()->_verify );
		$this->view ()->assign ( "rs", $rs );
		$this->view ()->display ( 'file:member/edit.tpl' );
	}
	
	/**
	 * 保存用户
	 */
	public function save() {
		
		$rs = self::getMemberService ()->save ( $_POST );
		self::getLogService ()->add ( $this->_user_global ['uid'], "编辑用户$_POST[uid]" );
		
		$this->redirect ( $rs, "./index.php?member/edit/?id=$_POST[uid]" );
	
	}
	
	/**
	 * 用户账户信息
	 * */
	public function account() {
		$userinfo = $this->getUserInfo ();
		$this->view ()->assign ( "userinfo", $userinfo );
		$this->view ()->display ( "mysql:account.tpl" );
	}
	
	/**
	 * 审核
	 * 
	 */
	public function setStatus() {
		$this->validteNode ( SUPER );
		self::getMemberService ()->saveStatus ( $_POST ['ids'], $_POST ['type'] );
		$this->sendNotice ( self::SUCEESS, NULL, true );
	
	}
	/**
	 * 修改密码 
	 */
	public function rePassword() {
		
		$this->view ()->display ( 'mysql:password.tpl' );
	
	}
	
	public function setPassword() {
		$this->isLogin ();
		$uid = $this->_user_global ['uid'];
		
		$message = self::getMemberService ()->rePasswordByUser ( $uid, $_POST ['password'], $_POST ['newPassword'] );
		self::getLogService ()->add ( $this->_user_global ['uid'], "修改密码" );
		$this->sendNotice ( $message );
	}
	
	public static function getLogService() {
		return new LogService ();
	}
	
	public static function getMemberService() {
		return new MemberService ();
	}
}